/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| sound.h: Sound class to hold all sounds, written to avoid memory leaks I
|	was having with the SFML library
\------------------------------------------------------------------------------------------------*/

#include "Sound.h"


//Initializing all game sounds
Sound::Sound()
{
	sf::SoundBuffer buffer1;
	if(!buffer1.LoadFromFile("select.wav"))
		exit(EXIT_FAILURE);
	for(int i = 0; i < SELECTSOUNDS; i++)
	{
		mSelectSound[i].SetBuffer(buffer1);
		mSelectSound[i].SetVolume(SFXVOLUME);
	}

	sf::SoundBuffer buffer2;
	if(!buffer2.LoadFromFile("enter.wav"))
		exit(EXIT_FAILURE);
	mEnterSound.SetBuffer(buffer2);
	mEnterSound.SetVolume(SFXVOLUME);

	sf::SoundBuffer buffer3;
	if(!buffer3.LoadFromFile("layzar.wav"))
		exit(EXIT_FAILURE);
	for(int i = 0; i < LAYZARSOUNDSP; i++)
	{
		mLayzarSoundP1[i].SetBuffer(buffer3);
		mLayzarSoundP2[i].SetBuffer(buffer3);
		mLayzarSoundP1[i].SetVolume(SFXVOLUME - 60);
		mLayzarSoundP2[i].SetVolume(SFXVOLUME - 60);
		mLayzarSoundP2[i].SetPitch((float)0.95);
	}

	sf::SoundBuffer buffer4;
	if(!buffer4.LoadFromFile("hit.wav"))
		exit(EXIT_FAILURE);
	for(int i = 0; i < HURTSOUNDSP; i++)
	{
		mHurtSoundP[i].SetBuffer(buffer4);
		mHurtSoundP[i].SetVolume(SFXVOLUME - 45);
	}

	for(int i = 0; i < HURTSOUNDSE; i++)
	{
		mHurtSoundE[i].SetBuffer(buffer4);
		mHurtSoundE[i].SetVolume(SFXVOLUME - 30);
	}

	for(int i = 0; i < LAYZARSOUNDSE; i++)
	{
		mLayzarSoundE[i].SetBuffer(buffer3);
		mLayzarSoundE[i].SetVolume(SFXVOLUME - 60);
	}

	sf::SoundBuffer buffer5;
	if(!buffer5.LoadFromFile("explosion.wav"))
		exit(EXIT_FAILURE);
	for(int i = 0; i < DIESOUNDS; i++)
	{
		mDieSound[i].SetBuffer(buffer5);
		mDieSound[i].SetVolume(SFXVOLUME - 55);
	}
}


//Finds the right sound to output... emumeration FTW
void Sound::playSound(int sound)
{
	switch(sound)
	{
	case SELECT:
		//Plays select sound from an appropriate buffer
		for(int i = 0; i < SELECTSOUNDS; i++)
		{
			if(mSelectSound[i].GetStatus() != sf::Sound::Playing)
			{
				mSelectSound[i].Play();
				i = SELECTSOUNDS;
			}
		}
		break;

	case ENTER:
		mEnterSound.Play();
		break;

	case LAYZAR_ONE:
		for(int i = 0; i < LAYZARSOUNDSP; i++)
		{
			if(mLayzarSoundP1[i].GetStatus() != sf::Sound::Playing)
			{
				mLayzarSoundP1[i].Play();
				i = LAYZARSOUNDSP;
			}
		}
		break;

	case LAYZAR_TWO:
		for(int i = 0; i < LAYZARSOUNDSP; i++)
		{
			if(mLayzarSoundP1[i].GetStatus() != sf::Sound::Playing)
			{
				mLayzarSoundP2[i].Play();
				i = LAYZARSOUNDSP;
			}
		}
		break;

	case HURT_PLAYER:
		for(int i = 0; i < HURTSOUNDSP; i++)
		{
			if(mHurtSoundP[i].GetStatus() != sf::Sound::Playing)
			{
				mHurtSoundP[i].Play();
				i = HURTSOUNDSP;
			}
		}
		break;

	case HURT_ENEMY:
		for(int i = 0; i < HURTSOUNDSE; i++)
		{
			if(mHurtSoundE[i].GetStatus() != sf::Sound::Playing)
			{
				mHurtSoundE[i].Play();
				i = HURTSOUNDSE;
			}
		}
		break;

	case LAYZAR_ENEMY:
		for(int i = 0; i < LAYZARSOUNDSE; i++)
		{
			if(mLayzarSoundE[i].GetStatus() != sf::Sound::Playing)
			{
				mLayzarSoundE[i].Play();
				i = LAYZARSOUNDSE;
			}
		}
		break;

	case DIE:
		for(int i = 0; i < DIESOUNDS; i++)
		{
			if(mDieSound[i].GetStatus() != sf::Sound::Playing)
			{
				mDieSound[i].Play();
				i = DIESOUNDS;
			}
		}
		break;
	}
}